using System.Collections.Generic;
using NOSDC.Pocos;
using NOSDC.Entities;
using System.Linq;

namespace NOSDC.Repositories
{
    public class CommentRepository : ICommentRepository
    {
        private NOSDCContext _nosdcContext;

        public CommentRepository()
        {
            _nosdcContext = new NOSDCContext();
        }

        public void PostComment(Comment comment)
        {
            _nosdcContext.Comments.AddObject(comment);
            _nosdcContext.SaveChanges();
        }

        public IList<Comment> GetCommentsByArticle(int articleID)
        {
            var comments = _nosdcContext.Comments
                                        .Where(c => c.ArticleID.Equals(articleID))
                                        .Where(c => c.StatusID.Equals(1))
                                        .OrderByDescending(c => c.DatePosted)
                                        .ToList();
            return comments;
        }


        public IList<Comment> GetCommentsByArticle(int articleID, int page, int pageSize)
        {
            var comments = _nosdcContext.Comments
                                        .Where(c => c.ArticleID.Equals(articleID))
                                        .Where(c => c.StatusID.Equals(1))
                                        .OrderByDescending(c => c.DatePosted)
                                        .Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return comments;
        }


        public int TotalComments(int articleID)
        {
            return _nosdcContext.Comments.Count(c => c.ArticleID.Equals(articleID));
        }
    }
}